import numpy as np
import seaborn as sns
from itertools import product

iris = sns.load_dataset('iris')

iris = iris[iris['species'].isin(['setosa', 'versicolor'])]

X = iris[['sepal_length', 'sepal_width', 'petal_width']].values
y = iris['petal_length'].values

def evaluate_model(X, y, kernel, C):

    if kernel == "linear":

        y_pred = np.dot(X, np.random.rand(X.shape[1]))
    elif kernel == "poly":

        y_pred = np.power(np.dot(X, np.random.rand(X.shape[1])) + 1, 2)
    elif kernel == "rbf":
        distances = np.sum((X[:, np.newaxis] - X) ** 2, axis=2)
        y_pred = np.exp(-distances / (2 * C ** 2)).mean(axis=1)
    else:
        raise ValueError("不支持的核类型")

    mse = np.mean((y - y_pred) ** 2)
    return mse

# 设置参数搜索范围
param_grid = {
    'kernel': ['linear', 'poly', 'rbf'],
    'C': [0.1, 1.0, 10.0],
}

# 初始化最佳参数和最佳分数
best_score = float('inf')
best_params = None

param_combinations = list(product(param_grid['kernel'], param_grid['C']))

# 遍历所有参数组合进行评估
for kernel, C in param_combinations:
    score = evaluate_model(X, y, kernel, C)
    print(f"Kernel: {kernel}, C: {C}, MSE: {score}")
    if score < best_score:
        best_score = score
        best_params = {'kernel': kernel, 'C': C}

print("Best parameters found:")
print(best_params)